//
//  JAnimatedView.m
//  iBuy
//
//  Created by junFung on 15/10/11.
//  Copyright (c) 2015年 yc. All rights reserved.
//

#import "JAnimatedView.h"

@implementation JAnimatedView
- (instancetype)initWithFrame:(CGRect)frame addToView:(UIView *)view
{
    self = [super init];
    if (self) {
        self.frame = frame;
        [view addSubview:self];
        self.alpha = 0.0;
        self.backgroundColor = [[UIColor whiteColor] colorWithAlphaComponent:0.90] ;
        NSArray *images = @[[UIImage imageNamed:@"loading_640x1136_1"],[UIImage imageNamed:@"loading_640x1136_2"],[UIImage imageNamed:@"loading_640x1136_3"],[UIImage imageNamed:@"loading_640x1136_4"]];
        
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"loading_640x1136_1"]];
        image.bounds = CGRectMake(0, 0, 63, 85);
        image.center = view.center;
        [self addSubview:image];
        
        image.animationDuration = 0.4;
        image.animationImages = images;
        image.animationRepeatCount = MAXFLOAT;
        [image startAnimating];
        
        UILabel *labelTip = [[UILabel alloc] init];
        labelTip.frame = CGRectMake(0, CGRectGetMaxY(image.frame)+5.0, screenW, 20);
        labelTip.text = @"正在努力加载中.....";
        labelTip.textColor = [UIColor darkGrayColor];
        labelTip.font = [UIFont systemFontOfSize:15.0];
        labelTip.textAlignment = NSTextAlignmentCenter;
        [self addSubview:labelTip];
    }
    return self;
}

- (void)show
{
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    }];
}
- (void)hide
{
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
